<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>AdminLTE 3 | Log in</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Font Awesome -->
  <link rel="stylesheet" href="../../plugins/fontawesome-free/css/all.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- icheck bootstrap -->
  <link rel="stylesheet" href="../../plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../dist/css/adminlte.min.css">
  <!-- Google Font: Source Sans Pro -->
 <!-- <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">-->
</head>
<body class="hold-transition login-page">
<div class="login-box">
  <div class="login-logo">
    <a href="../../index.html"><b>Admin</b>JD</a>
  </div>
  <!-- /.login-logo -->
  <div class="card">
    <div class="card-body login-card-body">
      <p class="login-box-msg">Sign in to start your session</p>

      <form onsubmit="return checkForm()" action="#" method="post">
        <div class="input-group mb-3">
          <input type="input" name="userName" class="form-control" placeholder="请输入户名">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-user"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input type="password" name="psw" class="form-control" placeholder="请输入密码">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
        </div>

        <div class="row">
          <div class="col-7">
            <input  name="valicode" class="form-control" placeholder="请输入验证码">
          </div>
          <img id="valiimg" class="col-5" src="/user/getValiCodeImg" alt="验证码"/>
        </div>
        <div class="input-group mb-3"></div>
        <div class="row">
          <div class="col-8">
            <div class="icheck-primary">
              <input type="checkbox" id="remember">
              <label for="remember">
                Remember Me
              </label>
            </div>
          </div>
          <!-- /.col -->
          <div class="col-4">
            <button type="submit" class="btn btn-primary btn-block " onsubmit="check()" id="sub">Sign In</button>
          </div>
          <!-- /.col -->
        </div>
        
      </form>

      <!--<div class="social-auth-links text-center mb-3">
        <p>- OR -</p>
        <a href="#" class="btn btn-block btn-primary">
          <i class="fab fa-facebook mr-2"></i> Sign in using Facebook
        </a>
        <a href="#" class="btn btn-block btn-danger">
          <i class="fab fa-google-plus mr-2"></i> Sign in using Google+
        </a>
      </div>-->
      <!-- /.social-auth-links -->

      <!--<p class="mb-1">
        <a href="forgot-password.html">I forgot my password</a>
      </p>-->
      <!--<p class="mb-0">
        <a href="register.html" class="text-center">Register a new membership</a>
      </p>-->
    </div>
    <!-- /.login-card-body -->
  </div>
</div>
<!-- /.login-box -->

<!-- jQuery -->
<script src="../../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="../../dist/js/adminlte.min.js"></script>
<script>

    function recover() {
        $("#sub").html("Sign In");
        $("#sub").removeAttr("disabled");
    }

    function checkForm(){
      if(""==$("input[name='userName']").val()){
            alert("用户名不能为空！");
            return false;
      }
      if(""==$("input[name='psw']").val()){
          alert("密码不能为空！");
          return false;
      }
      if(""==$("input[name='valicode']").val()){
          alert("验证码不能为空！");
          return false;
      }
      if($("#sub").attr("disabled")=="disabled"){
          return false;
      }
      $("#sub").attr("disabled","disabled");
      $("#sub").html("<i class=\"fas fa-1x fa-sync-alt fa-spin\"></i> Sign In");
      var userName=$("input[name='userName']").val();
      var psw=$("input[name='psw']").val();
      var valicode=$("input[name='valicode']").val();
      var rember=$("#remember").get(0).checked;

      $.post("http://localhost/user/adminLogin",{"userName":userName,"psw":psw,"valicode":valicode,"rember":rember},function (result) {
          recover();
          if(result.status!=200){
              alert(result.msg);
          }else{
              $(window).attr('location','http://localhost/admin');
          }

      }).fail(function () {
          recover();
          alert("登录异常");
      });
      return false;
  }

  $(function () {
      $("#valiimg").click(function () {
          $(this).attr("src","/user/getValiCodeImg?a="+Math.random());
      });
      //RememberMe
      var strcookie = document.cookie;//获取cookie字符串
      var arrcookie = strcookie.split(";");//分割
      //遍历匹配
      for ( var i = 0; i < arrcookie.length; i++) {
          var arr = arrcookie[i].split("=");
          if(arr[0].trim()=="userName"){
              //存储登录信息的cookie值
              cookieV=arr[1].trim();
              if(arr[1].trim()!=""){
                  $("input[name='userName']").val(cookieV);
                  $("#remember").attr("checked", 'checked');
              }else{
                  $("#remember").removeAttr("checked");
              }

          }
      }
  });
</script>
</body>
</html>
